<?php

namespace Apps\AppApi\Method\Search;


use PhalconPlus\Api\Common\ResultStatus;

use Apps\Common\Models\BeautyParlor as BeautyParlorModel;

use Apps\Common\Models\BeautyParlorTag;

use Apps\Common\Models\BeautyParlorTagInfo;
class BeautyParlor extends BaseSearch
{
    public function run()
    {
    	$keyword = $this->getDataItem('keyword');
    	$page = intval($this->getDataItem('page',1));
        if(empty($keyword)) {
	        $offset = ($page-1)*10;
	        $list = BeautyParlorModel::query()->where("bp_state = 1")->limit(10,$offset)->execute()->toArray();
	        foreach ($list as $k=>$v){
	        	unset($list[$k]['bp_coordinate']);
	        }
	        foreach ($list as $arr){
	        	foreach ($arr as $k=>$v){
	        		$k=str_replace("bp_","",$k);
	        		$arrs[$k]=$v;
	        		//var_dump($arrs);
	        	}
	        	$newarr[]=$arrs;
	        }
	        $list=$newarr;
	        foreach ($list as $k=>$v){
	        	$medal=$list[$k]['medal'];
	        	$list[$k]['medal_refund']=substr($medal,0,1);
	        	$list[$k]['medal_verify']=substr($medal,1,1);
	        	$list[$k]['cover']="http://img.meelier.com/".$list[$k]['cover'];
	        	$tagarr=BeautyParlorTag::query()
	        	->columns('i.tag_title')
	        	->leftJoin('Apps\Common\Models\BeautyParlorTagInfo','Apps\Common\Models\BeautyParlorTag.tag_id = i.tag_id','i')
	        	->where("Apps\Common\Models\BeautyParlorTag.bp_id = ".$list[$k]['id'])
	        	->orderby("Apps\Common\Models\BeautyParlorTag.sort asc")
	        	->limit(3,0)
	        	->execute()->toArray();
	        	$newarr=[];
	        	foreach ($tagarr as $k=>$v){
	        		$newarr[]=$tagarr[$k]['tag_title'];
	        	}
	        	$list[$k]['tags']=$newarr;
	        }
	        $this->setResult($list);
        }else{ 
        	/* $offset = ($page-1)*10;
        	$list = BeautyParlorModel::query()->where("bp_name like '%".$keyword."%' and bp_state = 1")->limit(10,$offset)->execute()->toArray();
        	foreach ($list as $k=>$v){
        		unset($list[$k]['bp_coordinate']);
        	}
        	foreach ($list as $arr){
        		foreach ($arr as $k=>$v){
        			$k=str_replace("bp_","",$k);
        			$arrs[$k]=$v;
        			//var_dump($arrs);
        		}
        		$newarr[]=$arrs;
        	}
        	$list=$newarr;
        	foreach ($list as $k=>$v){
        		$medal=$list[$k]['medal'];
        		$list[$k]['medal_refund']=substr($medal,0,1);
        		$list[$k]['medal_verify']=substr($medal,1,1);
        		$list[$k]['cover']="http://img.meelier.com/".$list[$k]['cover'];
        	}
        	$this->setResult($list); */
        	$wherearr=$this->getsql(trim($keyword));
        	if($wherearr['where']!=""){
	        	$offset = ($page-1)*10;
	        	$list = BeautyParlorModel::query()
	        	->where($wherearr['where'])
	        	->orderBy($wherearr['order'])
	        	->limit(10,$offset)
	        	->execute()->toArray();
	        	foreach ($list as $k=>$v){
	        		unset($list[$k]['bp_coordinate']);
	        	}
	        	foreach ($list as $arr){
	        		foreach ($arr as $k=>$v){
	        			$k=str_replace("bp_","",$k);
	        			$arrs[$k]=$v;
	        			//var_dump($arrs);
	        		}
	        		$newarr[]=$arrs;
	        	}
	        	$list=$newarr;
	        	foreach ($list as $k=>$v){
	        		$medal=$list[$k]['medal'];
	        		$list[$k]['medal_refund']=substr($medal,0,1);
	        		$list[$k]['medal_verify']=substr($medal,1,1);
	        		$list[$k]['cover']="http://img.meelier.com/".$list[$k]['cover'];
	        		unset($tmparr);
	        		$tmparr=BeautyParlorTag::query()
	        		->columns('i.tag_title')
	        		->leftJoin('Apps\Common\Models\BeautyParlorTagInfo','Apps\Common\Models\BeautyParlorTag.tag_id = i.tag_id','i')
	        		->where("Apps\Common\Models\BeautyParlorTag.bp_id = ".$list[$k]['id'])
	        		->orderby("Apps\Common\Models\BeautyParlorTag.sort asc")
	        		->limit(3,0)
	        		->execute()->toArray();
	        		unset($tag);
	        		foreach ($tmparr as $r){
	        			$tag[]=$r['tag_title'];
	        		}
	        		$list[$k]['tags']=$tag;
	        	}
        	}
        	$this->setResult($list);
        }
    }
    private function getsql($keyword){
    	$tmp=[];
    	$wherearr=[];
    	$newarr=[];
    	$keywordarr=explode(" ",$keyword);
    	foreach ($keywordarr as $k=>$v){
    		unset($tmp);
    		$tmp=BeautyParlorTagInfo::query()
    		->columns("tag_id")
    		->where("tag_title = '".$v."'")
    		->execute()->toArray();
    		if(count($tmp)>0){
    			//$arr['tag'][]=$tmp;
    			$tag_id=BeautyParlorTag::query()->columns("bp_id")->where("tag_id = ".$tmp[0]['tag_id']." and sort < 4")->execute()->toArray();
				if(count($tag_id)>0){
	    			foreach ($tag_id as $r){
	    				$newarr[]=$r['bp_id'];
	    			}
	    			$bp_ids=implode(",",$newarr);
	    			$wherearr[]="bp_id in ($bp_ids)";
	    			$order=" find_in_set(bp_id,'$bp_ids') ";
				}
    		}
    	}
    	foreach ($keywordarr as $k=>$v){
    		unset($tmp);
    		$tmp=BeautyParlorModel::query()
    		->columns("bp_name")
    		->where("bp_name like '%".$v."%'")
    		->execute()->toArray();
    		if(count($tmp)>0){
    			$wherearr[]="bp_name like '%".$v."%'";
    		}
    	}
    	foreach ($keywordarr as $k=>$v){
    		unset($tmp);
    		$tmp=BeautyParlorModel::query()
    		->columns("bp_address")
    		->where("bp_address like '%".$v."%'")
    		->execute()->toArray();
    		if(count($tmp)>0){
    			$wherearr[]="bp_address like '%".$v."%'";
    		}
    	}
    	foreach ($keywordarr as $k=>$v){
    		unset($tmp);
    		$tmp=BeautyParlorModel::query()
    		->columns("bp_intro")
    		->where("bp_intro like '%".$v."%'")
    		->execute()->toArray();
    		if(count($tmp)>0){
    			$wherearr[]="bp_intro like '%".$v."%'";
    		}
    	}
    	$where=implode(" or ",$wherearr);
    	if($where!=''){
    		$data['where']="(".$where.") and (bp_state = 1)";
    	}else{
    		$data['where']="";
    	}
    	if($order==''){
    		$order=" bp_id desc";
    	}
    	$data['order']=$order;
    	return $data;
    }
}